
var vm = new Vue({
    el: "#productVue",
    data: {
        product: [],        //商品信息
        supplier:[],        //商户信息
        score:[],           //评分
        product_image:[],   //商品图片
        specifications:[],  //商品规格
        big_image:'',       //显示大图
        star:'',
        discount:'',
        searchs: '',
        collect_message:'',
        collect_message_english:'',
        login: true,
        logout: false,
        show: false,
        loading: true,
        supplier_id:'',
        product_id:'',
        classify: '',
        user_id:'',
        web_token:'',
        city:'',
        specification:'',   //选择规格
        unit:'',            //单位
        specificationId:'', //选择规格ID
        price:'',           //单价
        total_price : 0,    //总价格(规格*单价*数量)
        tax : 0,            //增值税(单价*比例)
        total_tax : 0,      //总增值税(规格*单价*数量*比例)
        num : 1,            //选择数量
        pageNo: 1,          //评页码
        cart_id:'',
        paginate:'',
        changes:{
            type:''
        },
        oss_url:oss_url,
        nodata:'',
        tel:'',
        email:'',
        productImage:'',
        supplierImage:''
    },
    methods: {
        buyCar:function () {
            if (this.supplier.is_open!=1){
                layer.msg(this.supplier.supplier_name+',暂停营业!',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (this.num>this.product.stock){
                layer.msg('该商品库存不足',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (!this.user_id){
                location.href = "/userweb/index/login.html?productId="+this.product.id;
            }else {
                var insert = {
                    user_id:            this.user_id,
                    product_id:         this.product.id,
                    product_specif_id:  this.specificationId,
                    supplier_id:        this.supplier_id,
                    num:                this.num,
                    cart_id:            this.cart_id
                };
                var url = '/api/Purchase/shoppingCartInsert';    
                this.$http.post( url , insert , {
                    emulateJSON : true
                } ).then( function (res) {
                  /*  this.cart_id = res.data.id;
                    var product = {
                        cart_id:                this.cart_id,
                        product_id:             this.product.id,
                        product_name:           this.product.name,
                        product_name_english:   this.product.name_english,
                        product_image:          this.productImage,
                        specification:          this.specification,
                        price:                  this.price,
                        tax:                    this.tax,
                        total_tax:              this.total_tax,
                        total_price:            this.total_price,
                        num:                    this.num,
                        is_include_tax:         this.product.is_include_tax,
                        stock:                  this.product.stock
                    };
                    var data = {
                        supplier_id:            this.supplier.supplier_id,
                        supplier_name:          this.supplier.supplier_name,
                        supplier_name_english:  this.supplier.supplier_name_english,
                        image:                  this.supplierImage,
                        product:[product]
                    };
                    buy_car_add(data);*/
                    location.href = "/userweb/index/myShoppingCar.html?type=1";
                } , function ( res ) {

                } );
            }
        },
        orderNow:function () {
            if (this.supplier.is_open!=1){
                layer.msg(this.supplier.supplier_name+',暂停营业!',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (this.num>this.product.stock){
                layer.msg('该商品库存不足',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (!this.user_id){
                location.href = "/userweb/index/login.html?productId="+this.product.id;
            }else {
                var product = {
                    user_id:            this.user_id,
                    product_id:         this.product.id,
                    product_specif_id:  this.specificationId,
                    supplier_id:        this.supplier_id,
                    num:                this.num
                };
                localStorage.order_now = JSON.stringify(product);
                location.href = '/userweb/index/orderNow.html?ids='+this.product.id+'&type=2';
            }
        },
        collect: function (index,id) {
            if (!this.user_id ){
                location.href = "/userweb/index/login.html";
            }
            validToken(); //验证tonken.
            if (index == 1){
                var url = '/api/Collection/confirmCollectProduct';
                this.collect_message            = '收藏成功，您可以在收藏夹中查看该商品';
                this.collect_message_english    = 'Collection success, you can view this product in the favorites';
            }
            if (index == 2){
                var url = '/api/Collection/delProduct';
                this.collect_message            = '取消收藏成功';
                this.collect_message_english    = 'Cancel Collection success';
            }
            var data = {
                user_id:        this.user_id,
                web_token:      this.web_token,
                product_id:     id
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                //layer.msg(res.data.msg);
            }, function (res) {

            });
            this.req();
        },
        change1: function (index) {
            this.loading    = true;
            var url = '/api/Product/evaluate';
            if ( index == 1 ) {
                this.changes.type = '1';
                this.pageNo = 1;
            } else if ( index == 2 ) {
                this.changes.type = '2';
                this.pageNo = 1;
            } else if ( index == 3 ) {
                this.changes.type = '3';
                this.pageNo = 1;
            }
            var data = {
                user_id:        this.user_id,
                web_token:      this.web_token,
                page_no:        this.pageNo,
                type :          this.changes.type,
                product_id:     this.product_id,
                is_web:         1,
                page_size:      15
            };
            this.$http.post( url , data , {
                emulateJSON : true
            } ).then( function (res) {
                //console.log(res.data);
                if (res.data.status == 200) {
                    this.score      = res.data.data;
                    this.star       = star(Math.round(res.data.score * Math.pow(10, 0)) / Math.pow(10, 0));
                    this.paginate   = res.data.html;
                    if (this.score.length == 0){
                        $('#noData').html(this.nodata);
                    }else {
                        $('#noData').html("");
                    }
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                /*var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
                this.loading = false;
            } , function ( res ) {

            } );
        },
        change2: function (index) {
            if (isNaN(this.num)){
                layer.msg('请输入数字</br>Please enter a number',{
                    skin:'winning-class',
                    time:3000
                });
                this.num = 1;
            }
            this.num = parseInt(this.num);
            if (index == 1) {
                this.num -= 1;
            } else if (index == 2) {
                this.num += 1;
            }
            if (this.num > this.product.stock){
                this.num = this.product.stock;
            }
            if (this.num<1 || !this.num){
                this.num = 1;
            }
            var total_tax       = (parseFloat(this.specification) * parseFloat(this.tax) * parseFloat(this.num)).toFixed(2); //总增值税(规格*单价*数量*比例)
            var total_price     = (parseFloat(this.specification) * parseFloat(this.price) * parseFloat(this.num)).toFixed(2);
            this.total_tax      = total_tax;
            this.total_price    = (parseFloat(total_tax)+parseFloat(total_price)).toFixed(2);
        },
        req: function () {
            this.loading    = true;
            var url = '/api/Product/productInfo';
            var data = {
                user_id:     this.user_id,
                web_token:   this.web_token,
                type:'WEB',
                product_id:  this.product_id
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.supplier_id    = res.data.data.supplier;
                    this.product        = res.data.data;
                    this.product_image  = res.data.data.list_image;
                    this.specification  = res.data.data.packing_proportion;
                    this.unit           = res.data.data.unit;
                    if (this.specification == 1){
                        this.specification = ''
                    }
                    this.specificationId= res.data.data.fid;
                    this.price          = res.data.data.price;
                    this.supplier       = res.data.data.suppliers;
                    this.total_price    = (parseFloat(this.price) * parseFloat(this.specification) * parseFloat(this.num)).toFixed(2);
                    if (this.product.is_include_tax == 0){
                        this.tax        = (parseFloat(this.product.price) * parseFloat(this.product.tax)).toFixed(2);
                        this.total_tax  = (parseFloat(this.tax) * parseFloat(this.specification) * parseFloat(this.num)).toFixed(2);
                    }
                    this.total_price    = (parseFloat(this.total_price) + parseFloat(this.total_tax)).toFixed(2);
                    this.classify = 'category_id,'+res.data.data.category_id;
                    this.productImage  = this.product.image;
                    this.supplierImage = this.supplier.image;
                    if (this.product.image){
                        this.product.image = this.oss_url + this.product.image;
                    }
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
                this.supplier.image    = this.oss_url + this.supplier.image;
                for(var i = 0;i<this.product_image.info_url.length;i++){
                    this.product_image.info_url[i] = this.oss_url + this.product_image.info_url[i]
                }
                for(var k = 0;k<this.product_image.list_url.length;k++){
                    this.product_image.list_url[k] = this.oss_url + this.product_image.list_url[k]
                }
                if (this.product_image.list_url){
                    this.big_image = this.product_image.list_url[0]
                }
                /*var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
                this.loading = false;
                // console.log(this.supplier);
            }, function (res) {

            });

        },
        change : function (list) {
            this.price          = list.price;
            this.specification  = list.packing_proportion;
            this.unit           = list.unit;
            this.specificationId= list.id;
            if (list.packing_proportion == 1){
                this.specification = ''
            }
            if (this.product.is_include_tax == 0){
                this.tax            = (parseFloat(this.price)       * parseFloat(this.product.tax)).toFixed(2);                   //增值税(单价*比例)
                this.total_tax      = (parseInt(this.specification) * parseFloat(this.tax) * parseInt(this.num)).toFixed(2);     //总增值税(规格*单价*数量*比例)
            }
            this.total_price    = (parseInt(this.specification) * parseFloat(this.price) * parseInt(this.num)).toFixed(2);       //总价格(规格*单价*数量)
        },
        ini: function () {
            var url = '/userweb/Homepage/productExtend';
            var data = {
                user_id:     this.user_id,
                web_token:   this.web_token,
                type:'WEB',
                product_id:  this.product_id
            };
            this.$http.post(url, data, {
                headers:{
                    //token: token,
                    from:'USERWEB',
                    Method:'productExtend'
                },
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.discount        = res.data.discount;
                    this.specifications  = res.data.specification;
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {

            });
        },
        changeImage:function (url) {
            this.big_image = url;
        },
        showSelect:function(){
            if(this.show == false){
                $('.select_search_img').find('img').css('transform','rotate(180deg)');
                $('#baseData').css('display','block');
                this.show = true;
            }else {
                $('.select_search_img').find('img').css('transform','rotate(360deg)');
                $('#baseData').css('display','none');
                this.show = false;
            }
        },
        xhlData:function () {
            var url = '/userweb/Homepage/xhlData';
            var data = {
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.tel    = res.data.xhlData.tel;
                    this.email  = res.data.xhlData.email;
                } else {
                    layer.msg(res.data.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {

            });
        }
    },
    mounted: function () {
        this.nodata += '<div class="my_no-data">';
        this.nodata += '<div class="no_data_tip">';
        this.nodata += '<img src="/image/userweb/slicesImg/no-data.png" alt="">';
        this.nodata += '<div class="no-data-text">';
        this.nodata += '<div class="title_zh">暂无数据</div>';
        this.nodata += '<div class="title_en">No data</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        this.nodata += '</div>';
        var str = location.href; //取得整个地址栏
        this.product_id = getUrlParam(str,'productId');
        this.user_id    = localStorage.getItem('user_id');
        this.web_token  = localStorage.getItem('web_token');
        this.city       = localStorage.getItem('city');
        if (!this.city){
            this.city       = 'Devon County';
        }
        if (this.user_id){
            this.login  = false;
            this.logout = true;
        }else {
            this.login  = true;
            this.logout = false;
        }
        this.req();
        this.ini();
        this.xhlData();
    }
});
function ajaxPage(page) {
    vm.pageNo = page;
    vm.change1();
}
function jump(){
    var page = $("#page_no").val();
    vm.pageNo = page;
    vm.change1();
}